package com.ymatou.iapi.prodstock.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.prodstock.parameter.PreOrderReduceCatalogDto.CatalogListDto;
import com.ymatou.iapi.prodstock.service.ReduceStockListForPreOrderCall;
import com.ymatou.iapi.sellerproduct.service.AddFbxProductCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AdminCallService;
import com.ymttest.business.service.ProdStockCallService;
import com.ymttest.business.service.ProductPriceCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.mongo.jproduct.ActivityProductsWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductActivityStockWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;

public class Ts_ReduceStockListForPreOrder {
	// default data
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	
	private static YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
	private static ActivityProductsWapper activityProductsWapper  = new ActivityProductsWapper();
	private static YmtProductActivityStockWapper ymtProductActivityStockWapper = new YmtProductActivityStockWapper();
	
	private static ReduceStockListForPreOrderCall reduceProductStockForOrderCall = new ReduceStockListForPreOrderCall();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("预下单扣减库存");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(reduceProductStockForOrderCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReduceStockListForPreOrder_001() {
		Logger.start(true, "普通现货商品,扣减库存");
		try{
			String productId = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int reduceNum = 2;
			
			//扣库存
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId, sellerId, 0, reduceNum, 0, true);
			//check
			ProdStockCallService.checkReduceStock(catalogListDto,catalogNum-reduceNum,0);
			
			//扣库存 幂等
			CatalogListDto catalogListDto1 = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId, sellerId, 0, reduceNum, 0, 
											catalogListDto.getBuyerId(),catalogListDto.getPreOrderId(),true,true);
			//check
			ProdStockCallService.checkReduceStock(catalogListDto1,catalogNum-reduceNum,0);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReduceStockListForPreOrder_002() {
		Logger.start(true, "活动商品,没有限购,扣减库存");
		try {
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int reduceNum = 2;
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			// 扣库存
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,
					sellerId, aid, reduceNum, 0, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto, catalogNum, actNum-reduceNum);
			
			

			// 扣库存  幂等
			CatalogListDto catalogListDto1 = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,
					sellerId, aid, reduceNum, 0,catalogListDto.getBuyerId(), catalogListDto.getPreOrderId(),true,true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto1, catalogNum, actNum-reduceNum);
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReduceStockListForPreOrder_003() {
		Logger.start(true, "活动商品,活动限购,扣减库存");
		try {
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int reduceNum = 2;
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			
			//修改活动限购数量 商品限购
			ProdStockCallService.updateActivityLimit(aid, 0, 3);
			
			// 扣库存
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, reduceNum, 0,buyerId, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto, catalogNum, actNum-reduceNum);
						
			/////////////////////////////////////////////////////////////////////
			//第一个商品 不同买家下单
			YmtCatalogsI ymtCatalog2 = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId2 = ymtCatalog2.getsCatalogId();
			int catalogNum2 = ymtCatalog2.getiNum();
			int reduceNum2 = 2;
			int actNum2 = ymtProductActivityStockWapper.selectByCatalogId(catalogId2).getIstocknum();
			
			// 扣库存
			CatalogListDto catalogListDto2 = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId2,sellerId, aid, reduceNum2, 0, true);
			// check 扣成功
			ProdStockCallService.checkReduceStock(catalogListDto2, catalogNum2, actNum2-reduceNum2);


			
			/////////////////////////////////////////////////////////////////////
			//第二个商品报名 同一个买家下单
			String productId3 = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			//报名
			AdminCallService.applyAndApproveInActivity(aid, sellerId, productId3);
			
			YmtCatalogsI ymtCatalog3 = ymtCatalogsIWapper.selectCatalogsByProductId(productId3).get(0);
			String catalogId3 = ymtCatalog3.getsCatalogId();
			int catalogNum3 = ymtCatalog3.getiNum();
			int reduceNum3 = 2;
			int actNum3 = ymtProductActivityStockWapper.selectByCatalogId(catalogId3).getIstocknum();
			
			// 扣库存
			CatalogListDto catalogListDto3 = ProdStockCallService.ReduceStockListForPreOrder(productId3, catalogId3,sellerId, aid, reduceNum3, 0,buyerId, false);
			// check 超限购 没有扣成功
			ProdStockCallService.checkReduceStock(catalogListDto3, catalogNum3, actNum3,false);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReduceStockListForPreOrder_004() {
		Logger.start(true, "活动商品,活动商品限购,扣减库存");
		try {
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int reduceNum = 2;
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			
			//修改活动限购数量 活动商品限购
			ProdStockCallService.updateActivityLimit(aid, 1, 3);
			
			// 扣库存
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, reduceNum, 0,buyerId, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto, catalogNum, actNum-reduceNum);
						
			/////////////////////////////////////////////////////////////////////
			//第二个商品报名 同买家下单
			String productId2 = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			//报名
			AdminCallService.applyAndApproveInActivity(aid, sellerId, productId2);
			
			YmtCatalogsI ymtCatalog2 = ymtCatalogsIWapper.selectCatalogsByProductId(productId2).get(0);
			String catalogId2 = ymtCatalog2.getsCatalogId();
			int catalogNum2 = ymtCatalog2.getiNum();
			int reduceNum2 = 2;
			int actNum2 = ymtProductActivityStockWapper.selectByCatalogId(catalogId2).getIstocknum();
			
			// 扣库存
			CatalogListDto catalogListDto2 = ProdStockCallService.ReduceStockListForPreOrder(productId2, catalogId2,sellerId, aid, reduceNum2, 0,buyerId, true);
			// check 扣成功
			ProdStockCallService.checkReduceStock(catalogListDto2, catalogNum2, actNum2-reduceNum2);


			
			/////////////////////////////////////////////////////////////////////
			//第一个商品报名 同一个买家下单
			YmtCatalogsI ymtCatalog3 = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId3 = ymtCatalog3.getsCatalogId();
			int catalogNum3 = ymtCatalog3.getiNum();
			int reduceNum3 = 2;
			int actNum3 = ymtProductActivityStockWapper.selectByCatalogId(catalogId3).getIstocknum();
			
			// 扣库存
			CatalogListDto catalogListDto3 = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId3,sellerId, aid, reduceNum3, 0,buyerId, false);
			// check 超限购 没有扣成功
			ProdStockCallService.checkReduceStock(catalogListDto3, catalogNum3, actNum3,false);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReduceStockListForPreOrder_005() {
		Logger.start(true, "普通FBX商品,扣减库存");
		try {
			String productId = new AddFbxProductCall().callAndGetReturnData(MapUtil.hashMap("UserID", sellerId, "LoginID", sellerName)).getJSONObject("Data").getString("ProductID");
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int reduceNum = 2;
			
			//扣库存
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId, sellerId, 0, reduceNum, 2, true);
			//check
			ProdStockCallService.checkReduceStock(catalogListDto,catalogNum-reduceNum,0);
			
			
			//扣库存 幂等
			CatalogListDto catalogListDto1 = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId, sellerId, 0, reduceNum, 2, 
											catalogListDto.getBuyerId(),catalogListDto.getPreOrderId(),true,true);
			//check
			ProdStockCallService.checkReduceStock(catalogListDto1,catalogNum-reduceNum,0);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReduceStockListForPreOrder_006() {
		Logger.start(true, "FBX活动商品,扣减库存,没有限购");
		try {
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			
			//修改catalogType=2 FBX
			new YmtProductsIWapper().updateCatalogTypeByProductId(productId, 2);
			new YmtCatalogsIWapper().updateCatalogTypeByProductId(productId, 2);
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int reduceNum = 2;
			
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			//扣库存
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId, sellerId, aid, reduceNum, 2, true);
			//check
			ProdStockCallService.checkReduceStock(catalogListDto,catalogNum,actNum-reduceNum);
			
			

			// 扣库存  幂等
			CatalogListDto catalogListDto1 = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,
					sellerId, aid, reduceNum, 2,catalogListDto.getBuyerId(), catalogListDto.getPreOrderId(),true,true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto1, catalogNum, actNum-reduceNum);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReduceStockListForPreOrder_007() {
		Logger.start(true, "FBX活动商品,活动限购,扣减库存 ");
		try {
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			
			//修改catalogType=2 FBX
			new YmtProductsIWapper().updateCatalogTypeByProductId(productId, 2);
			new YmtCatalogsIWapper().updateCatalogTypeByProductId(productId, 2);
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int reduceNum = 2;
			
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			//修改活动限购数量 商品限购
			ProdStockCallService.updateActivityLimit(aid, 0, 3);
			
			//扣库存
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, reduceNum, 2,buyerId, true);
			//check
			ProdStockCallService.checkReduceStock(catalogListDto, catalogNum, actNum-reduceNum);
			
			//第一个商品 不同买家下单
			YmtCatalogsI ymtCatalog2 = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId2 = ymtCatalog2.getsCatalogId();
			int catalogNum2 = ymtCatalog2.getiNum();
			int reduceNum2 = 2;
			int actNum2 = ymtProductActivityStockWapper.selectByCatalogId(catalogId2).getIstocknum();

			// 扣库存
			CatalogListDto catalogListDto2 = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId2,sellerId, aid, reduceNum2, 2, true);
			// check 扣成功
			ProdStockCallService.checkReduceStock(catalogListDto2, catalogNum2, actNum2-reduceNum2);
			
			
			
			/////////////////////////////////////////////////////////////////////
			//第二个商品报名 同一个买家下单
			String productId3 = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			
			//报名
			AdminCallService.applyAndApproveInActivity(aid, sellerId, productId3);

			//修改catalogType=2 FBX
			new YmtProductsIWapper().updateCatalogTypeByProductId(productId3, 2);
			new YmtCatalogsIWapper().updateCatalogTypeByProductId(productId3, 2);
			
			YmtCatalogsI ymtCatalog3 = ymtCatalogsIWapper.selectCatalogsByProductId(productId3).get(0);
			String catalogId3 = ymtCatalog3.getsCatalogId();
			int catalogNum3 = ymtCatalog3.getiNum();
			int reduceNum3 = 2;
			int actNum3 = ymtProductActivityStockWapper.selectByCatalogId(catalogId3).getIstocknum();
			
			//扣库存
			CatalogListDto catalogListDto3 = ProdStockCallService.ReduceStockListForPreOrder(productId3, catalogId3,sellerId, aid, reduceNum3,2,buyerId, false);
			//check 超限购 没有扣成功
			ProdStockCallService.checkReduceStock(catalogListDto3, catalogNum3, actNum3,false);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReduceStockListForPreOrder_008() {
		Logger.start(true, "FBX活动商品,活动商品限购,扣减库存 ");
		try {
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			
			//修改catalogType=2 FBX
			new YmtProductsIWapper().updateCatalogTypeByProductId(productId, 2);
			new YmtCatalogsIWapper().updateCatalogTypeByProductId(productId, 2);
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int reduceNum = 2;
			
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			//修改活动限购数量 活动商品限购
			ProdStockCallService.updateActivityLimit(aid, 1, 3);
			
			// 扣库存
			CatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, reduceNum, 2,buyerId, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto, catalogNum, actNum-reduceNum);
			
			/////////////////////////////////////////////////////////////////////
			//第二个商品报名 同买家下单
			String productId2 = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			//报名
			AdminCallService.applyAndApproveInActivity(aid, sellerId, productId2);
			
			//修改catalogType=2 FBX
			new YmtProductsIWapper().updateCatalogTypeByProductId(productId2, 2);
			new YmtCatalogsIWapper().updateCatalogTypeByProductId(productId2, 2);
			
			
			YmtCatalogsI ymtCatalog2 = ymtCatalogsIWapper.selectCatalogsByProductId(productId2).get(0);
			String catalogId2 = ymtCatalog2.getsCatalogId();
			int catalogNum2 = ymtCatalog2.getiNum();
			int reduceNum2 = 2;
			int actNum2 = ymtProductActivityStockWapper.selectByCatalogId(catalogId2).getIstocknum();
			
			//扣库存
			CatalogListDto catalogListDto2 = ProdStockCallService.ReduceStockListForPreOrder(productId2, catalogId2,sellerId, aid, reduceNum2, 2,buyerId, true);
			//check 扣成功
			ProdStockCallService.checkReduceStock(catalogListDto2, catalogNum2, actNum2-reduceNum2);
			
			/////////////////////////////////////////////////////////////////////
			//第一个商品报名 同一个买家下单
			YmtCatalogsI ymtCatalog3 = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId3 = ymtCatalog3.getsCatalogId();
			int catalogNum3 = ymtCatalog3.getiNum();
			int reduceNum3 = 2;
			int actNum3 = ymtProductActivityStockWapper.selectByCatalogId(catalogId3).getIstocknum();
			
			//扣库存
			CatalogListDto catalogListDto3 = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId3,sellerId, aid, reduceNum3, 2,buyerId, false);
			//check 超限购 没有扣成功
			ProdStockCallService.checkReduceStock(catalogListDto3, catalogNum3, actNum3,false);

		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}
